package com.sqx.modules.health.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.modules.health.common.Constants;
import com.sqx.modules.health.common.Result;
import com.sqx.modules.health.entity.Dict;
import com.sqx.modules.health.entity.Menu;
import com.sqx.modules.health.mapper.DictMapper;
import com.sqx.modules.health.service.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单 前端控制器
 * </p>
 *
 * @author xglhappy
 * @since 2022-04-14
 */

@Api(value = "菜单", tags = "", description = "")
@RestController
@RequestMapping("/health/menu")
public class MenuController {
    @Autowired
    private IMenuService menuService;

    @Autowired
    private DictMapper dictMapper;

    // 新增或更新
    @PostMapping("/save")
    @ApiOperation(value = "新增或更新菜单", notes = "新增或更新菜单")
    public Result save(@RequestBody Menu menu){
        return Result.success(menuService.saveOrUpdate(menu));
    }

    // 根据ID删除
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据ID删除菜单", notes = "根据ID删除菜单")
    public Result delete(@PathVariable Integer id){
        return Result.success(menuService.removeById(id));
    }

    // 根据IDS删除
    @PostMapping("/del/batch")
    @ApiOperation(value = "根据IDS删除菜单", notes = "根据IDS删除菜单")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        return Result.success(menuService.removeByIds(ids));
    }

    // 查询全部
    @GetMapping("/findAll")
    @ApiOperation(value = "查询全部菜单", notes = "查询全部菜单")
    public Result findAll(){
        return Result.success(menuService.list());
    }

    // 根据ID查询一条记录
    @GetMapping("/findOne")
    @ApiOperation(value = "根据ID查询一条记录菜单", notes = "根据ID查询一条记录菜单")
    public Result findOne(@RequestParam Integer id){
        return Result.success(menuService.getById(id));
    }

    // 分页查询
    @GetMapping("/findPage")
    @ApiOperation(value = "分页查询菜单", notes = "分页查询菜单")
    public Result findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return  Result.success(menuService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }

    @GetMapping("/ids")
    public Result findAllIds() {
        return Result.success(menuService.list().stream().map(Menu::getId));
    }

    // 查询全部
    @GetMapping("/findTreeAll")
    @ApiOperation(value = "查询全部菜单", notes = "查询全部菜单")
    public Result findTreeAll(){
        return Result.success(menuService.findMenus(""));
    }

    @GetMapping("/icons")
    public Result getIcons() {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", Constants.DICT_TYPE_ICON);
        return Result.success(dictMapper.selectList(queryWrapper));
    }

}

